package coap.manager;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.applet.Applet;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: iodine
 * Date: Mar 17, 2012
 * Time: 7:16:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class ManagerForm extends Applet {

    JButton log = new JButton("Log/Logout...");
    JLabel emersonLogo = new JLabel("Emerson");
    JLabel utcsLogo = new JLabel("UTCS");
    JLabel coapManager = new JLabel("CoAP Manager");

    JTabbedPane main = new JTabbedPane();
    JTable table = null;
    DefaultTableModel model = new DefaultTableModel();


    JPanel settings = new JPanel();
    JLabel pollingFreq = new JLabel("Polling Frequency");
    JTextField pollingFreqText = new JTextField();
    JLabel warningTemp = new JLabel("Warning Temperature");
    JTextField warningTempText = new JTextField();
    JButton save = new JButton("Save");

    @Override
    public void init() {
        this.setLayout(null);
        log.setBounds(20, 20, 150, 30);
        this.add(log);
        Font font = new Font("Verdana", Font.BOLD, 18);
        emersonLogo.setFont(font);
        emersonLogo.setBounds(20, 150, 150, 30);
        this.add(emersonLogo);
        utcsLogo.setFont(font);
        utcsLogo.setBounds(20, 300, 150, 30);
        this.add(utcsLogo);

        font = new Font("Verdana", Font.BOLD, 24);
        coapManager.setFont(font);
        coapManager.setBounds(400, 20, 400, 30);
        this.add(coapManager);


        table = new JTable(model);
        table.setDefaultRenderer(Object.class, new CustomTableCellRenderer());
        model.addColumn("NodeID");
        model.addColumn("IP");
        model.addColumn("Temperature");
        model.addRow(new String[]{"1234", "127.0.0.1", "23"});
        model.addRow(new String[]{"1235", "127.0.0.1", "58"});

        JScrollPane scrollPane = new JScrollPane(table);
        main.add("nodes", scrollPane);

        settings.setLayout(null);
        pollingFreq.setBounds(20, 20, 160, 20);
        settings.add(pollingFreq);
        pollingFreqText.setBounds(200, 20, 100, 20);
        settings.add(pollingFreqText);

        warningTemp.setBounds(20, 60, 160, 20);
        settings.add(warningTemp);
        warningTempText.setBounds(200,60, 100, 20);
        settings.add(warningTempText);
        save.setBounds(20, 100, 100, 20);
        settings.add(save);

        main.add("settings", settings);
        main.setBounds(200, 50, 600, 400);
        this.add(main);

    }

    public class CustomTableCellRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent
                (JTable table, Object value, boolean isSelected,
                 boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent
                    (table, value, isSelected, hasFocus, row, column);
            //System.out.println(row + " " + column + " " + value);
            if (column == 2) {
                Integer temp = Integer.parseInt((String) value);
                if (temp.intValue() > 50) {
                    cell.setBackground(Color.red);
                    // You can also customize the Font and Foreground this way
                    // cell.setForeground();
                    // cell.setFont();
                } else {
                    cell.setBackground(Color.white);
                }
            }
            else
                cell.setBackground(Color.white);
            return cell;
        }
    }
}
